package com.aiyige.base.api;

import android.util.Log;

import com.aiyige.BuildConfig;
import com.aiyige.MyApp;
import com.aiyige.base.api.httpinterceptor.ApiHeaderInterceptor;
import com.aiyige.base.api.httpinterceptor.AuthTokenInterceptor;
import com.aiyige.base.api.httpinterceptor.AuthIntercetpor;
import com.aiyige.base.api.httpinterceptor.CrashReportInterceptor;
import com.aiyige.base.api.httpinterceptor.FollowInterceptor;
import com.aiyige.base.api.httpinterceptor.TokenInterceptor;
import com.aiyige.page.home.model.FollowItem;
import com.zhy.http.okhttp.cookie.CookieJarImpl;
import com.zhy.http.okhttp.cookie.store.PersistentCookieStore;
import com.zhy.http.okhttp.https.HttpsUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by xiachao on 2017/8/10.
 */

public class OkHttpUtil {
    public static final String TAG = OkHttpUtil.class.getSimpleName();

    public static OkHttpClient createOkHttpClientForCrashReport(long timeoutS) {
        ApiHeaderInterceptor aiyigeHeaderInterceptor = new ApiHeaderInterceptor();
        TokenInterceptor tokenInterceptor = new TokenInterceptor();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        CrashReportInterceptor crashReportInterceptor = new CrashReportInterceptor();
        if (BuildConfig.DEBUG) {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        } else {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        }
        CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(MyApp.getAppContext()));
        InputStream[] certInputStream = new InputStream[1];
        try {
            certInputStream[0] = MyApp.getAppContext().getAssets().open("dance365com.cer");
        } catch (IOException e) {
            Log.e(TAG, "getOkHttpClient:" + Log.getStackTraceString(e));
            return null;
        }
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(certInputStream, null, null);
        return new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .connectTimeout(timeoutS, TimeUnit.SECONDS)
                .readTimeout(timeoutS, TimeUnit.SECONDS)
                .writeTimeout(timeoutS, TimeUnit.SECONDS)
                .cookieJar(cookieJar)
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .addInterceptor(aiyigeHeaderInterceptor)
                .build();
    }

    public static OkHttpClient createOkHttpClient(long timeoutS) {
        ApiHeaderInterceptor aiyigeHeaderInterceptor = new ApiHeaderInterceptor();
        TokenInterceptor tokenInterceptor = new TokenInterceptor();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        AuthIntercetpor authIntercetpor = new AuthIntercetpor();
        AuthTokenInterceptor authTokenInterceptor = new AuthTokenInterceptor();
        CrashReportInterceptor crashReportInterceptor = new CrashReportInterceptor();
        FollowInterceptor followInterceptor = new FollowInterceptor();
        if (BuildConfig.DEBUG) {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        } else {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        }
        CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(MyApp.getAppContext()));
        InputStream[] certInputStream = new InputStream[1];
        try {
            certInputStream[0] = MyApp.getAppContext().getAssets().open("dance365com.cer");
        } catch (IOException e) {
            Log.e(TAG, "getOkHttpClient:" + Log.getStackTraceString(e));
            return null;
        }
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(certInputStream, null, null);
        return new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .connectTimeout(timeoutS, TimeUnit.SECONDS)
                .readTimeout(timeoutS, TimeUnit.SECONDS)
                .writeTimeout(timeoutS, TimeUnit.SECONDS)
                .cookieJar(cookieJar)
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .addInterceptor(authIntercetpor)
                .addInterceptor(authTokenInterceptor)
                .addInterceptor(aiyigeHeaderInterceptor)
                .addInterceptor(followInterceptor)
                .addInterceptor(loggingInterceptor)
                .addInterceptor(crashReportInterceptor)
                .build();
    }


    public static OkHttpClient createOkHttpClientForAuth(long timeoutS) {
        ApiHeaderInterceptor aiyigeHeaderInterceptor = new ApiHeaderInterceptor();
        AuthTokenInterceptor authTokenInterceptor = new AuthTokenInterceptor();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        if (BuildConfig.DEBUG) {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        } else {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        }
        CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(MyApp.getAppContext()));
        InputStream[] certInputStream = new InputStream[1];
        try {
            certInputStream[0] = MyApp.getAppContext().getAssets().open("dance365com.cer");
        } catch (IOException e) {
            Log.e(TAG, "getOkHttpClient:" + Log.getStackTraceString(e));
            return null;
        }
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(certInputStream, null, null);
        return new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .connectTimeout(timeoutS, TimeUnit.SECONDS)
                .readTimeout(timeoutS, TimeUnit.SECONDS)
                .writeTimeout(timeoutS, TimeUnit.SECONDS)
                .cookieJar(cookieJar)
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .addInterceptor(aiyigeHeaderInterceptor)
                .addInterceptor(loggingInterceptor)
                .build();
    }


}
